package driving;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrabNrxDataByProxy {
	static String imgPath = "nrx";
	public String baseUrl = "http://lady.97jie.net/B/HunWaiYouQing/";
	private String soruce = "女人巷";
	private int count = 0;
	
	public static void main(String[] args) throws Exception{
		GrabNrxDataByProxy gkd = new GrabNrxDataByProxy();
		gkd.getNav();
    }
	
	
	/**
	 * 获取头部导航菜单
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getNav() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		Map<String, String> pages = new HashMap<String, String>();
		pages.put("http://lady.97jie.net/B/HunWaiYouQing/69.html", "口述实录");
		pages.put("http://lady.97jie.net/B/QingGanGuShi/14.html", "情感故事");
		pages.put("http://lady.97jie.net/B/ZhenBianHua/15.html", "枕边话");
		pages.put("http://lady.97jie.net/B/PoXiGuanXi/07.html", "婆媳关系");
		pages.put("http://lady.97jie.net/B/HunYinWuYu/35.html", "婚姻物语");
		pages.put("http://lady.97jie.net/B/LianAiBaoDian/22.html", "恋爱宝典");
		Map<String, Long> titles = getTitleAndId();
        for(String key : pages.keySet()) {
	        String pageTotal = key.substring(key.lastIndexOf("/") + 1, key.lastIndexOf("."));
	        for(int i = 1; i<Integer.valueOf(pageTotal); i++) {
	        	String url = i==1 ? key.substring(0, key.indexOf("/" + pageTotal)) + "/" : key.replaceAll(pageTotal, i+"");
	        	boolean rs = true;
	        	String tn = pages.get(key);
	        	rs = getPage(url, tn, titles);
	        	if(!rs) break;
	        }
        }
        System.out.println("本次抓取数据：" + count);
	}
	
	/**
	 * 获取分页数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public boolean getPage(String url, String typeName, Map<String, Long> titles) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		System.out.println("start get page data!" + typeName + "====" + url);
		Document doc = GenericGrap.getDoc(url, "gb2312");
		Elements els = doc.getElementById("JaList").getElementsByTag("li");
		for(Element el : els) {
			try {
				Element titleEl = el.getElementsByTag("a").get(0);
				String title = titleEl.text();
				try {

					Element tagEl = el.getElementsByAttributeValue("class", "aList-label").get(0);
					if(null != tagEl && tagEl.text().trim().length() > 0) {
						String tags = tagEl.text().trim().replaceAll(" ", ",").replaceAll(" ", "");
						Long id = titles.get(title);
						if(null != id) {
						HandleContentByJsonup.updateTag(id, tags);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
        return true;
	}
	
	public Map<String, Long> getTitleAndId() {
		Map<String, Long> articles = new HashMap<String, Long>();
		try {
			Connection connection = DbUtil.getQgshConn();
			Statement statement = connection.createStatement();
			String source = "and source='"+soruce+"' ";
			String sql="select id,title from d_article_grap where "
					+ " 1=1 " + source;
					;
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				articles.put(rs.getString("title"), rs.getLong("id"));
			}
			rs.close();
			statement.close();
//			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
}
